package com.project.springboot.controller;

import com.gitee.yongzhuzl.commonutil.util.result.ResponseResult;
import com.project.springboot.VO.CupVo;
import com.project.springboot.entity.Cup;
import com.project.springboot.service.cup.CupService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * description: 杯子 WEB API
 * @author 朱正磊
 * @date 2023-09-19 19:02:50
 */
@RestController
@RequestMapping("/cup")
public class CupController {

    @Resource
    private CupService cupService;

    @PostMapping("/addCup")
    public ResponseResult<Void> addCup(@RequestBody Cup cup) {
        cupService.addCup(cup);
        return ResponseResult.buildSuccess();
    }

    @GetMapping("/listCup")
    public ResponseResult<List<CupVo>> listCup() {
        List<CupVo> list = cupService.listCup();
        return ResponseResult.buildSuccess(list);
    }

    /**
     * 会成功插入2条杯子数据和1条学生数据，因为在try代码块中，抛出异常之前的代码是会被正常执行的，抛出异常之后的代码不会被执行到
     * 因为在本Service里面的方法提取跟不提取其实没有区别，this.insertStudent();方法调用和直接写代码逻辑作用没有差别！！！
     * description: 测试Transaction rolled back because it has been marked as rollback-only
     * @author 朱正磊
     * @date 2024-05-22 15:30:33
     */
    @GetMapping("/testTransactionalRollback1")
    public ResponseResult<Void> testTransactionalRollback1() {
        cupService.testTransactionalRollback1();
        return ResponseResult.buildSuccess();
    }

    /**
     * 不会插入任何的杯子数据和学生数据，会抛出
     * org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only
     * 因为try代码块中调用了其他Service方法，而且抛出异常的代码是在其他Service方法里，同时满足这两个条件就会触发该异常
     * description: 测试Transaction rolled back because it has been marked as rollback-only
     * @author 朱正磊
     * @date 2024-05-22 15:30:33
     */
    @GetMapping("/testTransactionalRollback2")
    public ResponseResult<Void> testTransactionalRollback2() {
        cupService.testTransactionalRollback2();
        return ResponseResult.buildSuccess();
    }

    /**
     * 会成功插入2条杯子数据和1条学生数据，因为在try代码块中，抛出异常之前的代码是会被正常执行的，抛出异常之后的代码不会被执行到
     * 虽然本Service里面调用了其他Service的方法，但是是在本Service里面抛出的异常，不是在其他Service里面抛出的异常
     * description: 测试Transaction rolled back because it has been marked as rollback-only
     * @author 朱正磊
     * @date 2024-05-22 15:30:33
     */
    @GetMapping("/testTransactionalRollback3")
    public ResponseResult<Void> testTransactionalRollback3() {
        cupService.testTransactionalRollback3();
        return ResponseResult.buildSuccess();
    }

    /**
     * 不涉及任何的增删查改，就只是ServiceA的方法调用ServiceB的方法，发现照样会抛出Transaction silently rolled back because it has been marked as rollback-only
     * 说明了只要满足ServiceA的方法调用ServiceB的方法，并且ServiceB的方法抛出了异常，ServiceA的方法对ServiceB的方法进行了try catch，但是catch里面没有对异常进行处理比如继续抛出或者手动回滚，
     * 就会出现Transaction silently rolled back because it has been marked as rollback-only
     * description: 测试Transaction rolled back because it has been marked as rollback-only
     * @author 朱正磊
     * @date 2025-06-21 13:05:44
     */
    @GetMapping("/testTransactionalRollback4")
    public ResponseResult<Void> testTransactionalRollback4() {
        cupService.testTransactionalRollback4();
        return ResponseResult.buildSuccess();
    }

    /**
     * 说明：使用Spring事务传播机制PROPAGATION_NESTED
     * 看现象是不是：nested 的子异常抛出 如果被catch 外部插入成功
     * description: 测试Transaction rolled back because it has been marked as rollback-only
     * @author 朱正磊
     * @date 2025-10-22 09:19:05
     */
    @GetMapping("/testTransactionalRollback5")
    public ResponseResult<Void> testTransactionalRollback5() {
        cupService.testTransactionalRollback5();
        return ResponseResult.buildSuccess();
    }

    /**
     * 说明：使用Spring事务传播机制PROPAGATION_NESTED
     * 看现象是不是：nested 的子异常抛出 如果不被catch 外部插入失败
     * description: 测试Transaction rolled back because it has been marked as rollback-only
     * @author 朱正磊
     * @date 2025-10-22 09:19:05
     */
    @GetMapping("/testTransactionalRollback6")
    public ResponseResult<Void> testTransactionalRollback6() {
        cupService.testTransactionalRollback6();
        return ResponseResult.buildSuccess();
    }

    /**
     * 说明：使用Spring事务传播机制PROPAGATION_NESTED
     * 看现象是不是：nested外面异常抛出 nest内部插入成功 nest也会跟着回滚
     * description: 测试Transaction rolled back because it has been marked as rollback-only
     * @author 朱正磊
     * @date 2025-10-22 09:19:05
     */
    @GetMapping("/testTransactionalRollback7")
    public ResponseResult<Void> testTransactionalRollback7() {
        cupService.testTransactionalRollback7();
        return ResponseResult.buildSuccess();
    }

}
